package com.xigua.a01;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/3/2716:18
 */
@SpringBootApplication
public class App {

    public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException, IOException {
        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
        Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
        singletonObjects.setAccessible(true);
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        Map<String,Object> map =(Map<String,Object>)singletonObjects.get(beanFactory);

        map.entrySet().stream().filter(e->e.getKey().startsWith("component")).forEach(e->{
            System.out.println(e.getKey()+"="+e.getValue());
                });

        Resource resources[] = context.getResources("classpath*:META-INF/spring.factories");
        for (Resource resource : resources) {
            System.out.println(resource);
        }

        System.out.println(context.getEnvironment().getProperty("java_home"));
        System.out.println(context.getEnvironment().getProperty("server.port"));


//        context.publishEvent(new UserRegisteredEvent(context));
        context.getBean(Component1.class).register();
    }

}
